package com.example.desinger.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.desinger.common.R;
import com.example.desinger.entity.HomeRemark;
import com.example.desinger.entity.HomeWork;
import com.example.desinger.entity.StuHomeWork;
import com.example.desinger.service.HomeRemarkService;
import com.example.desinger.service.HomeWorkService;
import com.example.desinger.service.StuHomeWorkService;
import jakarta.websocket.server.PathParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RequestMapping("/hw")
@RestController
public class HomeWorkController {
    @Autowired
    private HomeWorkService homeWorkService;

    @Autowired
    private StuHomeWorkService stuHomeWorkService;

    @Autowired
    private HomeRemarkService homeRemarkService;

    //文件的路径
    @Value("${designer.path}")
    private String basePath;
    /*
    * 添加作业
    * //老师为不同的班创建文件作业
    * */
    @PostMapping
    public R<String> saveHome(@RequestBody HomeWork homeWork){
        //老师为不同的班创建文件作业
        log.info("取来的homework的值为：{}",homeWork);

        return homeWorkService.saveAndProfile(homeWork);
    }

    /*
    * 上传文件到本地实现，只需要修改yml路径即可
    * */
    @PostMapping("/stu/upload")
    public R<String>upload(MultipartFile file,String stu_id,String class_id,String home_work_id){
        //转换
        int stuId = Integer.parseInt(stu_id);
        int classId = Integer.parseInt(class_id);
        int homeWorkId = Integer.parseInt(home_work_id);
        R<String> stringR =  homeWorkService.uploadFile(file,stuId,classId,homeWorkId);
        return stringR;
    }

    //老师给作业评分
    @PostMapping("/score")
    public R<String> scoreByTeacher(@RequestBody StuHomeWork stuHomeWork){
        System.out.println(stuHomeWork);
        R<String> stringR = stuHomeWorkService.updateScoreById(stuHomeWork);
        return stringR;
    }


    //学生评论作业
    @PostMapping("/stu/remark")
    public R<String> remarkStudentHW(@RequestBody HomeRemark homeRemark){
       R<String> stringR = homeRemarkService.remarkStudentOfHW(homeRemark);
        return stringR;

    }

    //学生查找自己的作业
    @GetMapping("/stu")
    public R<List<StuHomeWork>> selectHWById(@PathParam("classId") int classId, @PathParam("stuId") int stuId){

        R<List<StuHomeWork>> listR =  stuHomeWorkService.selectHWById(classId,stuId);
        return listR;
    }

        @GetMapping("/class")
        public R<List<HomeWork>> findById(@PathParam("classId")int classId){
           List<HomeWork> homeWork  =  homeWorkService.findById(classId);
        return R.success(homeWork,"查询成功！");
        }

    //修改学生的标签
    @PutMapping("/interfile")
    public R<String> upDate(@RequestBody StuHomeWork stuHomeWork){
        System.out.println(stuHomeWork);
        return stuHomeWorkService.upDateSelf(stuHomeWork);
    }

    @GetMapping("/all")
    public R<List<StuHomeWork>> findAll(int classId,int homeWorkId){
        LambdaQueryWrapper<StuHomeWork> lambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StuHomeWork::getClassId,classId)
                        .eq(StuHomeWork::getHomeWorkId,homeWorkId);
        List<StuHomeWork> list = stuHomeWorkService.list(lambdaQueryWrapper);
        return R.success(list,"这个班的这次实验的作业情况如下：");
    }

    @DeleteMapping
    public R<String> delectById(@PathParam("id") int id){
        return  homeWorkService.delectHomeWordById(id);
    }


}
